######## DATA SET RECODING AND SCALE CREATION FOR DOST, ENOS, HOCHSCHILD: PSQ 2020 ########

#### SET-UP ####
## set working directory
setwd("data and replication")
## load packages
library(haven)
library(car)
library(data.table)

##### LOAD 2017 DATA AND RECODE FOR ANALYSIS #####
# 2017 data set
d17 <- read_sav("original_datasets/HarvardCAPS_HarrisPoll_Sept2017.sav")
## RENAME VARIABLES
names(d17)[names(d17) == 'Propwts'] <- 'weight'
names(d17)[names(d17) == 'Q4005'] <- 'sex'
names(d17)[names(d17) == 'QNEW'] <- 'race'
names(d17)[names(d17) == 'Q4007'] <- 'age'
names(d17)[names(d17) == 'QM12'] <- 'ideo'
names(d17)[names(d17) == 'Q2157'] <- 'income'
## sex dummies
d17$male <- ifelse(d17$sex==1, 1, 0)
d17$Men <- ifelse(d17$sex==1, 1, 0)
d17$Women <- ifelse(d17$sex==2, 1, 0)
## race 
# values: 1'White NH'2'Black NH'3'Hispanic'4'OtherNH'
d17$racerec <- recode(d17$race, "1=1;2=2;3=3;4:5=4")
## race dummies 
d17$WhiteNH <- ifelse(d17$racerec==1, 1, 0)
d17$BlackNH <- ifelse(d17$racerec==2, 1, 0)  
d17$Hispanic <- ifelse(d17$racerec==3, 1, 0)
d17$OthNH <- ifelse(d17$racerec==4, 1, 0)
## age categories
# values: 1'18-29'2'30-49'3'50-64'4'65+'
d17$agerec <- recode(d17$age, "18:29=1; 30:49=2; 50:64=3; 65:max(d17$age)=4")
## age dummies 
d17$Age_18to29 <- ifelse(d17$agerec==1, 1, 0)
d17$Age_30to49 <- ifelse(d17$agerec==2, 1, 0)  
d17$Age_50to64 <- ifelse(d17$agerec==3, 1, 0)
d17$Age_65plus <- ifelse(d17$agerec==4, 1, 0)
## education categories
# values: 1'HS degree or less'2'Some coll or Assoc deg or Post-HS training'3'Coll grad'4'Post grad work or more'
d17$educ <- recode(d17$Q2146, "1:3=1; 4=2; 70:max(d17$Q2146)=2; 5=3; 6:7=4")
## educ dummies 
d17$Educ_HSorLess <- ifelse(d17$educ==1, 1, 0)
d17$Educ_SomeColl <- ifelse(d17$educ==2, 1, 0)  
d17$Educ_CollGrad <- ifelse(d17$educ==3, 1, 0)
d17$Educ_PostGrad <- ifelse(d17$educ==4, 1, 0)
## income categories
d17$increc_4 <- recode(d17$income, "1:3=1;4:5=2;6:7=3;8:11=4;99=NA")
## income dummies
d17$Inc_125kplus <- ifelse(d17$income>=8 & d17$income<=11, 1, 0)
d17$Inc_75to125k <- d17$Inc_125kplus <- ifelse(d17$income>=6 & d17$income<=7, 1, 0)
d17$Inc_35to75k <- d17$Inc_125kplus <- ifelse(d17$income>=4 & d17$income<=5, 1, 0)
d17$Inc_under35k <- d17$Inc_125kplus <- ifelse(d17$income<=3, 1, 0)
d17$Inc_125kplus[d17$income==99] <- NA
d17$Inc_75to125k[d17$income==99] <- NA
d17$Inc_35to75k[d17$income==99] <- NA
d17$Inc_under35k[d17$income==99] <- NA
## employment groups
# values: # 1=full-time; 2=part-time; 3=stay-at-home/unable/retired/student/not looking; 4=not employed, looking
d17$employ <- recode(d17$Q410, "c(1,3)=1;c(2,15)=2;5:9=3;4=4")
## 7-point party ID variable
# values: 1'Strong Rep' 2'Not so strong Rep' 3'Lean Rep' 4'No lean ind' 5'Lean Dem' 6'Not so strong Dem' 7'Strong Dem'
d17$ptystr <- NA
d17$ptystr[d17$Q1905==1] <- 1
d17$ptystr[d17$Q1905==2] <- 2
d17$ptystr[d17$Q1915==1] <- 3
d17$ptystr[d17$Q1915==3] <- 4
d17$ptystr[d17$Q1915==2] <- 5
d17$ptystr[d17$Q1910==2] <- 6
d17$ptystr[d17$Q1910==1] <- 7
## partyID dummies 
d17$StrRep <- ifelse(d17$ptystr==1, 1, 0)
d17$NotStrRep <- ifelse(d17$ptystr==2, 1, 0)  
d17$LeanRep <- ifelse(d17$ptystr==3, 1, 0)
d17$PureInd <- ifelse(d17$ptystr==4, 1, 0)
d17$LeanDem <- ifelse(d17$ptystr==5, 1, 0)
d17$NotStrDem <- ifelse(d17$ptystr==6, 1, 0)  
d17$StrDem <- ifelse(d17$ptystr==7, 1, 0)
## party leaned variable
# values: 1'Rep/Lean Rep'2'Dem/Lean Dem'3'Pure Ind'
d17$ptylean <- recode(d17$ptystr, "1:3=1; 4=3; 5:7=2")
## 3-point party ID variable
# values: 1'Rep'2'Ind'3'Dem'
d17$pty3 <- recode(d17$ptystr, "1:2=1; 3:5=2; 6:7=3")
## political ideology dummies
d17$StrLib <- ifelse(d17$ideo==1, 1, 0)
d17$LeanLib <- ifelse(d17$ideo==2, 1, 0)  
d17$Moderate <- ifelse(d17$ideo==3, 1, 0)
d17$LeanConserv <- ifelse(d17$ideo==4, 1, 0)
d17$StrConserv <- ifelse(d17$ideo==5, 1, 0)
## 2016 presidential vote choice variable
# values: 1'Trump'2'Clinton'3'Voted other'4'Refused'
d17$howvote <- recode(d17$QS4, "2=1; 1=2; 3:5=3; 6=4")
## Trump support/approval variable
d17$trump_sup <- 0
d17$trump_sup[d17$QM3<=2] <- 1
## Our analytical groups
# values: 1'Rep Loyalists'2'Rep Switchers'3'Dem Switchers'4'Dem Loyalists'
d17$stay_leave <- NA
d17$stay_leave[d17$howvote==1 & d17$trump_sup==1] <- 1
d17$stay_leave[d17$howvote==1 & d17$trump_sup==0] <- 2
d17$stay_leave[d17$howvote!=1 & d17$howvote!=4 & d17$trump_sup==1] <- 3
d17$stay_leave[d17$howvote!=1 & d17$howvote!=4 & d17$trump_sup==0] <- 4


##### CREATE SCALES FOR 2017 DATA #####
### consolidating "is it racist" variables
## reverse coding
d17$QRA4_2rec <- recode(d17$QRA4_2, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_4rec <- recode(d17$QRA4_4, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_6rec <- recode(d17$QRA4_6, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_8rec <- recode(d17$QRA4_8, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_10rec <- recode(d17$QRA4_10, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_12rec <- recode(d17$QRA4_12, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_14rec <- recode(d17$QRA4_14, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_16rec <- recode(d17$QRA4_16, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_18rec <- recode(d17$QRA4_18, "5=1;4=2;3=3;2=4;1=5")
d17$QRA4_20rec <- recode(d17$QRA4_20, "5=1;4=2;3=3;2=4;1=5")
## combining
d17$QRA4_1_sum <- NA
d17$QRA4_1_sum[!is.na(d17$QRA4_1)] <- d17$QRA4_1[!is.na(d17$QRA4_1)]
d17$QRA4_1_sum[!is.na(d17$QRA4_2rec)] <- d17$QRA4_2rec[!is.na(d17$QRA4_2rec)]
d17$QRA4_3_sum <- NA
d17$QRA4_3_sum[!is.na(d17$QRA4_3)] <- d17$QRA4_3[!is.na(d17$QRA4_3)]
d17$QRA4_3_sum[!is.na(d17$QRA4_4rec)] <- d17$QRA4_4rec[!is.na(d17$QRA4_4rec)]
d17$QRA4_5_sum <- NA
d17$QRA4_5_sum[!is.na(d17$QRA4_5)] <- d17$QRA4_5[!is.na(d17$QRA4_5)]
d17$QRA4_5_sum[!is.na(d17$QRA4_6rec)] <- d17$QRA4_6rec[!is.na(d17$QRA4_6rec)]
d17$QRA4_7_sum <- NA
d17$QRA4_7_sum[!is.na(d17$QRA4_7)] <- d17$QRA4_7[!is.na(d17$QRA4_7)]
d17$QRA4_7_sum[!is.na(d17$QRA4_8rec)] <- d17$QRA4_8rec[!is.na(d17$QRA4_8rec)]
d17$QRA4_9_sum <- NA
d17$QRA4_9_sum[!is.na(d17$QRA4_9)] <- d17$QRA4_9[!is.na(d17$QRA4_9)]
d17$QRA4_9_sum[!is.na(d17$QRA4_10rec)] <- d17$QRA4_10rec[!is.na(d17$QRA4_10rec)]
d17$QRA4_11_sum <- NA
d17$QRA4_11_sum[!is.na(d17$QRA4_11)] <- d17$QRA4_11[!is.na(d17$QRA4_11)]
d17$QRA4_11_sum[!is.na(d17$QRA4_12rec)] <- d17$QRA4_12rec[!is.na(d17$QRA4_12rec)]
d17$QRA4_13_sum <- NA
d17$QRA4_13_sum[!is.na(d17$QRA4_13)] <- d17$QRA4_13[!is.na(d17$QRA4_13)]
d17$QRA4_13_sum[!is.na(d17$QRA4_14rec)] <- d17$QRA4_14rec[!is.na(d17$QRA4_14rec)]
d17$QRA4_15_sum <- NA
d17$QRA4_15_sum[!is.na(d17$QRA4_15)] <- d17$QRA4_15[!is.na(d17$QRA4_15)]
d17$QRA4_15_sum[!is.na(d17$QRA4_16rec)] <- d17$QRA4_16rec[!is.na(d17$QRA4_16rec)]
d17$QRA4_17_sum <- NA
d17$QRA4_17_sum[!is.na(d17$QRA4_17)] <- d17$QRA4_17[!is.na(d17$QRA4_17)]
d17$QRA4_17_sum[!is.na(d17$QRA4_18rec)] <- d17$QRA4_18rec[!is.na(d17$QRA4_18rec)]
d17$QRA4_19_sum <- NA
d17$QRA4_19_sum[!is.na(d17$QRA4_19)] <- d17$QRA4_19[!is.na(d17$QRA4_19)]
d17$QRA4_19_sum[!is.na(d17$QRA4_20rec)] <- d17$QRA4_20rec[!is.na(d17$QRA4_20rec)]
### recoding economic ladder questions
d17$qra8_ac <- d17$QRA8_1 - d17$QRA8_3
d17$qra8_ab <- d17$QRA8_1 - d17$QRA8_2
d17$qra8_bc <- d17$QRA8_2 - d17$QRA8_3
### recoding variables
vars_to_rec <- c("QRA2_1","QRA2_2","QRA2_3","QRA2_4","QRA2_5","QRA2_6","QRA3_1","QRA3_2","QRA3_3",
                 "QRA3_4","QRA3_5","QRA3_6","QRA4_1_sum","QRA4_3_sum","QRA4_5_sum","QRA4_7_sum",
                 "QRA4_9_sum","QRA4_11_sum","QRA4_13_sum","QRA4_15_sum","QRA4_17_sum","QRA4_19_sum",
                 "qra8_ac","qra8_ab","qra8_bc","QRA26_1","QRA26_2","QRA26_3","QRA26_4","QRA26_5",
                 "QRA26_6","QRA26_7","QRA26_8","QRA29","QRA30_1","QRA30_2","QRA30_3","QRA30_5",
                 "QRA30_6","QRA30_7","QRA30_8","QRA30_9","QRA30_10","QRA30_11","QRA30_12","QRA30_13",
                 "QRA30_14","QRA30_16","QRA31_1","QRA31_2","QRA31_3","QRA31_5","QRA31_6","QRA31_7",
                 "QRA31_8","QRA31_9","QRA31_10","QRA31_11","QRA31_12","QRA31_13","QRA31_14","QRA31_16",
                 "QRA33_1","QRA34","QRA35")
rec_var_names <- c("SOCIAL_DISTANCER1","SOCIAL_DISTANCER2","SOCIAL_DISTANCER3","SOCIAL_DISTANCER4",
                  "SOCIAL_DISTANCER5","SOCIAL_DISTANCER6","COSMOPOLITANISMR1","COSMOPOLITANISMR2",
                  "COSMOPOLITANISMR3","COSMOPOLITANISMR4","COSMOPOLITANISMR5","COSMOPOLITANISMR6",
                  "RACISM_OKR1","RACISM_OKR2","RACISM_OKR3","RACISM_OKR4","RACISM_OKR5","RACISM_OKR6",
                  "RACISM_OKR7","RACISM_OKR8","RACISM_OKR9","RACISM_OKR10","ECON_LADDER_past_future",
                  "ECON_LADDER_past_present","ECON_LADDER_present_future","TRULY_AMERICANR1","TRULY_AMERICANR2",
                  "TRULY_AMERICANR3","TRULY_AMERICANR4","TRULY_AMERICANR5","TRULY_AMERICANR6","TRULY_AMERICANR7",
                  "TRULY_AMERICANR8","PARENTS","TAXESR1","TAXESR2","TAXESR3","TAXESR4","TAXESR5","TAXESR6",
                  "TAXESR7","TAXESR8","TAXESR9","TAXESR10","TAXESR11","TAXESR12","TAXESR13","TAXESR14",
                  "LEGISLATIONR1","LEGISLATIONR2","LEGISLATIONR3","LEGISLATIONR4","LEGISLATIONR5","LEGISLATIONR6",
                  "LEGISLATIONR7","LEGISLATIONR8","LEGISLATIONR9","LEGISLATIONR10","LEGISLATIONR11","LEGISLATIONR12",
                  "LEGISLATIONR13","LEGISLATIONR14","COMPROMISE","US_DARK","BREAK_RULES")
setnames(d17, old = vars_to_rec, new = rec_var_names)
rm(rec_var_names, vars_to_rec)
### getting vars ready for analysis
d17$US_DARK_rec <- ifelse(d17$US_DARK==1, 1, 0)
d17$country_wrongtrack <- ifelse(d17$QM1==2, 1, 0)
d17$country_wrongtrack[d17$QM1==3] <- NA
d17$econ_wrongtrack <- ifelse(d17$QM2==2, 1, 0)
d17$econ_wrongtrack[d17$QM2==3] <- NA
d17$econ_strength <- car::recode(d17$QI3, "4=0;3=0.33;2=0.67;1=1")
d17$personalfinance <- car::recode(d17$QI4, "2=0;3=0.5;1=1")
d17$personalfinance[d17$personalfinance==4] <- NA
d17$BREAK_RULES_rec <- car::recode(d17$BREAK_RULES, "4=1;3=2;2=3;1=4")
d17$PARENTS_rec <- car::recode(d17$PARENTS, "5=0;4=.25;3=.5;2=.75;1=1")
d17$PARENTS_rec[d17$PARENTS==6] <- NA
d17$CHILDREN_rec <- car::recode(d17$QRA28, "5=0;4=.25;3=.5;2=.75;1=1")
d17$CHILDREN_rec[d17$QRA28==6] <- NA
d17$trump_immig_app <- car::recode(d17$QM3A_6, "1=-1;2=-0.33;3=0.33;4=1")
d17$trump_disapp_econ <- d17$QM3A_1
d17$trump_disapp_stimjobs <- d17$QM3A_5
#### creating scales 
# cosmopolitanism scale 
d17$COSMOPOLITANISMR1_rec2 <- recode(d17$COSMOPOLITANISMR1, "1=1;2=1;3=0;4=-1;5=-1")
d17$COSMOPOLITANISMR2_rec2 <- recode(d17$COSMOPOLITANISMR2, "1=1;2=1;3=0;4=-1;5=-1")
d17$COSMOPOLITANISMR3_rec2 <- recode(d17$COSMOPOLITANISMR3, "1=1;2=1;3=0;4=-1;5=-1")
d17$COSMOPOLITANISMR5_rec2 <- recode(d17$COSMOPOLITANISMR5, "1=1;2=1;3=0;4=-1;5=-1")
d17$cosmo_scale2 <- (d17$COSMOPOLITANISMR1_rec2 + d17$COSMOPOLITANISMR2_rec2 + d17$COSMOPOLITANISMR3_rec2 + d17$COSMOPOLITANISMR5_rec2)/4
# creating two is_it_racist scales
d17$RACISM_OKR1_rec <- car::recode(d17$RACISM_OKR1, "1=-1;2=-1;3=0;4=1;5=1")
d17$RACISM_OKR3_rec <- car::recode(d17$RACISM_OKR3, "1=-1;2=-1;3=0;4=1;5=1")
d17$RACISM_OKR4_rec <- car::recode(d17$RACISM_OKR4, "1=-1;2=-1;3=0;4=1;5=1")
d17$RACISM_OKR9_rec <- car::recode(d17$RACISM_OKR9, "1=-1;2=-1;3=0;4=1;5=1")
d17$is_it_racist_scale1 <- (d17$RACISM_OKR1_rec + d17$RACISM_OKR3_rec + d17$RACISM_OKR4_rec + d17$RACISM_OKR9_rec)/4
d17$RACISM_OKR5_rec <- car::recode(d17$RACISM_OKR5, "1=-1;2=-1;3=0;4=1;5=1")
d17$RACISM_OKR6_rec <- car::recode(d17$RACISM_OKR6, "1=-1;2=-1;3=0;4=1;5=1")
d17$is_it_racist_scale2 <- (d17$RACISM_OKR5_rec + d17$RACISM_OKR6_rec)/2
# creating two American scales
d17$TRULY_AMERICANR4_rec <- recode(d17$TRULY_AMERICANR4, "1=-1;2=-1;3=1;4=1")
d17$TRULY_AMERICANR5_rec <- recode(d17$TRULY_AMERICANR5, "1=-1;2=-1;3=1;4=1")
d17$TRULY_AMERICANR7_rec <- recode(d17$TRULY_AMERICANR7, "1=-1;2=-1;3=1;4=1")
d17$american_scale_v1 <- (d17$TRULY_AMERICANR4_rec + d17$TRULY_AMERICANR5_rec + d17$TRULY_AMERICANR7_rec)/3
d17$TRULY_AMERICANR3_rec <- recode(d17$TRULY_AMERICANR3, "1=-1;2=-1;3=1;4=1")
d17$TRULY_AMERICANR6_rec <- recode(d17$TRULY_AMERICANR6, "1=-1;2=-1;3=1;4=1")
d17$TRULY_AMERICANR8_rec <- recode(d17$TRULY_AMERICANR8, "1=-1;2=-1;3=1;4=1")
d17$american_scale_v2 <- (d17$TRULY_AMERICANR3_rec + d17$TRULY_AMERICANR6_rec + d17$TRULY_AMERICANR8_rec)/3
# creating first taxes scale (blacks, Hispanics, illegal immigrants) 
d17$TAXESR2_rec <- recode(d17$TAXESR2, "1=-1;2=-1;3=0;4=1;5=1")
d17$TAXESR3_rec <- recode(d17$TAXESR3, "1=-1;2=-1;3=0;4=1;5=1")
d17$TAXESR10_rec <- recode(d17$TAXESR10, "1=-1;2=-1;3=0;4=1;5=1")
d17$taxes_scale_v1 <- (d17$TAXESR2_rec + d17$TAXESR3_rec + d17$TAXESR10_rec)/3
# creating second taxes scale (wealthy people, large corps) 
d17$TAXESR8_rec <- recode(d17$TAXESR8, "1=-1;2=-1;3=0;4=1;5=1")
d17$TAXESR13_rec <- recode(d17$TAXESR13, "1=-1;2=-1;3=0;4=1;5=1")
d17$taxes_scale_v2 <- (d17$TAXESR8_rec + d17$TAXESR13_rec)/2
# creating first laws favorable scale (blacks, Hispanics, illegal immigrants)
d17$LEGISLATIONR2rec <- recode(d17$LEGISLATIONR2, "1=1;2=0;3=-1")
d17$LEGISLATIONR3rec <- recode(d17$LEGISLATIONR3, "1=1;2=0;3=-1")
d17$LEGISLATIONR10rec <- recode(d17$LEGISLATIONR10, "1=1;2=0;3=-1")
d17$laws_toofavorable <- (d17$LEGISLATIONR2rec + d17$LEGISLATIONR3rec + d17$LEGISLATIONR10rec)/3
# creating second laws favorable scale (wealthy people, large corps) 
d17$LEGISLATIONR8rec <- recode(d17$LEGISLATIONR8, "1=1;2=0;3=-1")
d17$LEGISLATIONR13rec <- recode(d17$LEGISLATIONR13, "1=1;2=0;3=-1")
d17$laws_toofavorable_wealthycorps <- (d17$LEGISLATIONR8rec + d17$LEGISLATIONR13rec)/2

#### saving out recoded dataset for analyses 
write.table(d17, "cleaned_datasets/harris_dec2017.csv", row.names=F, sep=",")



##### LOAD 2019 DATA AND RECODE FOR ANALYSIS #####
# load 2019 data set
d19 <- read_sav("original_datasets/HarvardCAPS_HarrisPoll_Feb2019.sav")
## RENAME VARIABLES
names(d19)[names(d19) == 'Propwts'] <- 'weight'
names(d19)[names(d19) == 'Q4005'] <- 'sex'
names(d19)[names(d19) == 'QRACE'] <- 'race'
names(d19)[names(d19) == 'Q4007'] <- 'age'
names(d19)[names(d19) == 'QM12'] <- 'ideo'
names(d19)[names(d19) == 'Q2157'] <- 'income'
## sex dummies
d19$male <- ifelse(d19$sex==1, 1, 0)
d19$Men <- ifelse(d19$sex==1, 1, 0)
d19$Women <- ifelse(d19$sex==2, 1, 0)
## race 
# values: 1'White NH'2'Black NH'3'Hispanic'4'OtherNH'
d19$racerec <- recode(d19$race, "1=1;2=2;3=3;4:5=4")
## race dummies 
d19$WhiteNH <- ifelse(d19$racerec==1, 1, 0)
d19$BlackNH <- ifelse(d19$racerec==2, 1, 0)  
d19$Hispanic <- ifelse(d19$racerec==3, 1, 0)
d19$OthNH <- ifelse(d19$racerec==4, 1, 0)
## age categories
# values: 1'18-29'2'30-49'3'50-64'4'65+'
d19$agerec <- recode(d19$age, "18:29=1; 30:49=2; 50:64=3; 65:max(d19$age)=4")
## age dummies 
d19$Age_18to29 <- ifelse(d19$agerec==1, 1, 0)
d19$Age_30to49 <- ifelse(d19$agerec==2, 1, 0)  
d19$Age_50to64 <- ifelse(d19$agerec==3, 1, 0)
d19$Age_65plus <- ifelse(d19$agerec==4, 1, 0)
## education categories
# values: 1'HS degree or less'2'Some coll or Assoc deg or Post-HS training'3'Coll grad'4'Post grad work or more'
d19$educ <- recode(d19$Q2146, "1:3=1; 4=2; 70:max(d19$Q2146)=2; 5=3; 6:7=4")
## educ dummies 
d19$Educ_HSorLess <- ifelse(d19$educ==1, 1, 0)
d19$Educ_SomeColl <- ifelse(d19$educ==2, 1, 0)  
d19$Educ_CollGrad <- ifelse(d19$educ==3, 1, 0)
d19$Educ_PostGrad <- ifelse(d19$educ==4, 1, 0)
## income categories
d19$increc_4 <- recode(d19$income, "1:3=1;4:5=2;6:7=3;8:11=4;99=NA")
## income dummies
d19$Inc_125kplus <- ifelse(d19$income>=8 & d19$income<=11, 1, 0)
d19$Inc_75to125k <- d19$Inc_125kplus <- ifelse(d19$income>=6 & d19$income<=7, 1, 0)
d19$Inc_35to75k <- d19$Inc_125kplus <- ifelse(d19$income>=4 & d19$income<=5, 1, 0)
d19$Inc_under35k <- d19$Inc_125kplus <- ifelse(d19$income<=3, 1, 0)
d19$Inc_125kplus[d19$income==99] <- NA
d19$Inc_75to125k[d19$income==99] <- NA
d19$Inc_35to75k[d19$income==99] <- NA
d19$Inc_under35k[d19$income==99] <- NA
## employment groups
# values: # 1=full-time; 2=part-time; 3=stay-at-home/unable/retired/student/not looking; 4=not employed, looking
d19$employ <- recode(d19$Q410, "c(1,3)=1;c(2,15)=2;5:9=3;4=4")
## 7-point party ID variable
# values: 1'Strong Rep' 2'Not so strong Rep' 3'Lean Rep' 4'No lean ind' 5'Lean Dem' 6'Not so strong Dem' 7'Strong Dem'
d19$ptystr <- NA
d19$ptystr[d19$Q1905==1] <- 1
d19$ptystr[d19$Q1905==2] <- 2
d19$ptystr[d19$Q1915==1] <- 3
d19$ptystr[d19$Q1915==3] <- 4
d19$ptystr[d19$Q1915==2] <- 5
d19$ptystr[d19$Q1910==2] <- 6
d19$ptystr[d19$Q1910==1] <- 7
## partyID dummies 
d19$StrRep <- ifelse(d19$ptystr==1, 1, 0)
d19$NotStrRep <- ifelse(d19$ptystr==2, 1, 0)  
d19$LeanRep <- ifelse(d19$ptystr==3, 1, 0)
d19$PureInd <- ifelse(d19$ptystr==4, 1, 0)
d19$LeanDem <- ifelse(d19$ptystr==5, 1, 0)
d19$NotStrDem <- ifelse(d19$ptystr==6, 1, 0)  
d19$StrDem <- ifelse(d19$ptystr==7, 1, 0)
## party leaned variable
# values: 1'Rep/Lean Rep'2'Dem/Lean Dem'3'Pure Ind'
d19$ptylean <- recode(d19$ptystr, "1:3=1; 4=3; 5:7=2")
## 3-point party ID variable
# values: 1'Rep'2'Ind'3'Dem'
d19$pty3 <- recode(d19$ptystr, "1:2=1; 3:5=2; 6:7=3")
## political ideology dummies
d19$StrLib <- ifelse(d19$ideo==1, 1, 0)
d19$LeanLib <- ifelse(d19$ideo==2, 1, 0)  
d19$Moderate <- ifelse(d19$ideo==3, 1, 0)
d19$LeanConserv <- ifelse(d19$ideo==4, 1, 0)
d19$StrConserv <- ifelse(d19$ideo==5, 1, 0)
## 2016 presidential vote choice variable
# values: 1'Trump'2'Clinton'3'Voted other'4'Refused'
d19$howvote <- recode(d19$QS4, "2=1; 1=2; 3:5=3; 6=4")
## Trump support/approval variable
d19$trump_sup <- 0
d19$trump_sup[d19$QM3<=2] <- 1
## Our analytical groups
# values: 1'Rep Loyalists'2'Rep Switchers'3'Dem Switchers'4'Dem Loyalists'
d19$stay_leave <- NA
d19$stay_leave[d19$howvote==1 & d19$trump_sup==1] <- 1
d19$stay_leave[d19$howvote==1 & d19$trump_sup==0] <- 2
d19$stay_leave[d19$howvote!=1 & d19$howvote!=4 & d19$trump_sup==1] <- 3
d19$stay_leave[d19$howvote!=1 & d19$howvote!=4 & d19$trump_sup==0] <- 4


##### CREATE SCALES FOR 2019 DATA #####
### recoding economic ladder questions
d19$ECON_LADDER_past_future <- d19$ECONOMIC_LADDERC1 - d19$ECONOMIC_LADDERC3
d19$ECON_LADDER_past_present <- d19$ECONOMIC_LADDERC1 - d19$ECONOMIC_LADDERC2
d19$ECON_LADDER_present_future <- d19$ECONOMIC_LADDERC2 - d19$ECONOMIC_LADDERC3
### getting vars ready for analysis
d19$US_DARK_rec <- ifelse(d19$US_DARK==1, 1, 0)
d19$country_wrongtrack <- ifelse(d19$QM1==2, 1, 0)
d19$country_wrongtrack[d19$QM1==3] <- NA
d19$econ_wrongtrack <- ifelse(d19$QM2==2, 1, 0)
d19$econ_wrongtrack[d19$QM2==3] <- NA
d19$econ_strength <- car::recode(d19$QI3, "4=0;3=0.33;2=0.67;1=1")
d19$personalfinance <- car::recode(d19$QI4, "2=0;3=0.5;1=1")
d19$personalfinance[d19$personalfinance==4] <- NA
d19$BREAK_RULES_rec <- car::recode(d19$BREAK_RULES, "4=1;3=2;2=3;1=4")
d19$PARENTS_rec <- car::recode(d19$PARENTS, "5=0;4=.25;3=.5;2=.75;1=1")
d19$PARENTS_rec[d19$PARENTS==6] <- NA
d19$LEGISLATIONR1 <- recode(d19$LEGISLATIONR1, "1=1;2=0;3=-1")
d19$LEGISLATIONR2 <- recode(d19$LEGISLATIONR2, "1=1;2=0;3=-1")
d19$LEGISLATIONR3 <- recode(d19$LEGISLATIONR3, "1=1;2=0;3=-1")
d19$LEGISLATIONR4 <- recode(d19$LEGISLATIONR4, "1=1;2=0;3=-1")
d19$LEGISLATIONR5 <- recode(d19$LEGISLATIONR5, "1=1;2=0;3=-1")
d19$LEGISLATIONR6 <- recode(d19$LEGISLATIONR6, "1=1;2=0;3=-1")
d19$LEGISLATIONR7 <- recode(d19$LEGISLATIONR7, "1=1;2=0;3=-1")
d19$LEGISLATIONR8 <- recode(d19$LEGISLATIONR8, "1=1;2=0;3=-1")
d19$LEGISLATIONR9 <- recode(d19$LEGISLATIONR9, "1=1;2=0;3=-1")
d19$LEGISLATIONR10 <- recode(d19$LEGISLATIONR10, "1=1;2=0;3=-1")
d19$LEGISLATIONR11 <- recode(d19$LEGISLATIONR11, "1=1;2=0;3=-1")
d19$LEGISLATIONR12 <- recode(d19$LEGISLATIONR12, "1=1;2=0;3=-1")
d19$LEGISLATIONR13 <- recode(d19$LEGISLATIONR13, "1=1;2=0;3=-1")
d19$LEGISLATIONR14 <- recode(d19$LEGISLATIONR14, "1=1;2=0;3=-1")
d19$LEGISLATIONR15 <- recode(d19$LEGISLATIONR15, "1=1;2=0;3=-1")
d19$CHILDREN_rec <- recode(d19$CHILDREN, "3=0;2=0.5;1=1")
d19$CHILDREN_rec[d19$CHILDREN >= 4] <- NA
d19$trump_immig_app <- recode(d19$QM3AR6, "1=-1;2=-0.33;3=0.33;4=1")
d19$trump_disapp_econ <- d19$QM3AR1
d19$trump_disapp_stimjobs <- d19$QM3AR5
#### creating scales 
# cosmopolitanism scale 
d19$COSMOPOLITANISMR1_rec2 <- recode(d19$COSMOPOLITANISMR1, "1=1;2=1;3=0;4=-1;5=-1")
d19$COSMOPOLITANISMR2_rec2 <- recode(d19$COSMOPOLITANISMR2, "1=1;2=1;3=0;4=-1;5=-1")
d19$COSMOPOLITANISMR3_rec2 <- recode(d19$COSMOPOLITANISMR3, "1=1;2=1;3=0;4=-1;5=-1")
d19$COSMOPOLITANISMR5_rec2 <- recode(d19$COSMOPOLITANISMR5, "1=1;2=1;3=0;4=-1;5=-1")
d19$cosmo_scale2 <- (d19$COSMOPOLITANISMR1_rec2 + d19$COSMOPOLITANISMR2_rec2 + d19$COSMOPOLITANISMR3_rec2 + d19$COSMOPOLITANISMR5_rec2)/4
# two American scales
d19$TRULY_AMERICANR4_rec <- recode(d19$TRULY_AMERICANR4, "1=-1;2=-1;3=1;4=1")
d19$TRULY_AMERICANR5_rec <- recode(d19$TRULY_AMERICANR5, "1=-1;2=-1;3=1;4=1")
d19$TRULY_AMERICANR7_rec <- recode(d19$TRULY_AMERICANR7, "1=-1;2=-1;3=1;4=1")
d19$american_scale_v1 <- (d19$TRULY_AMERICANR4_rec + d19$TRULY_AMERICANR5_rec + d19$TRULY_AMERICANR7_rec)/3
d19$TRULY_AMERICANR3_rec <- recode(d19$TRULY_AMERICANR3, "1=-1;2=-1;3=1;4=1")
d19$TRULY_AMERICANR6_rec <- recode(d19$TRULY_AMERICANR6, "1=-1;2=-1;3=1;4=1")
d19$TRULY_AMERICANR8_rec <- recode(d19$TRULY_AMERICANR8, "1=-1;2=-1;3=1;4=1")
d19$american_scale_v2 <- (d19$TRULY_AMERICANR3_rec + d19$TRULY_AMERICANR6_rec + d19$TRULY_AMERICANR8_rec)/3
# first taxes scale (blacks, Hispanics, illegal immigrants) 
d19$TAXESR2_rec <- recode(d19$TAXESR2, "1=-1;2=-1;3=0;4=1;5=1")
d19$TAXESR3_rec <- recode(d19$TAXESR3, "1=-1;2=-1;3=0;4=1;5=1")
d19$TAXESR10_rec <- recode(d19$TAXESR10, "1=-1;2=-1;3=0;4=1;5=1")
d19$taxes_scale_v1 <- (d19$TAXESR2_rec + d19$TAXESR3_rec + d19$TAXESR10_rec)/3
# second taxes scale (wealthy people, large corps) 
d19$TAXESR8_rec <- recode(d19$TAXESR8, "1=-1;2=-1;3=0;4=1;5=1")
d19$TAXESR13_rec <- recode(d19$TAXESR13, "1=-1;2=-1;3=0;4=1;5=1")
d19$taxes_scale_v2 <- (d19$TAXESR8_rec + d19$TAXESR13_rec)/2
# first laws favorable scale (blacks, Hispanics, illegal immigrants)
d19$LEGISLATIONR2rec <- recode(d19$LEGISLATIONR2, "1=1;2=0;3=-1")
d19$LEGISLATIONR3rec <- recode(d19$LEGISLATIONR3, "1=1;2=0;3=-1")
d19$LEGISLATIONR10rec <- recode(d19$LEGISLATIONR10, "1=1;2=0;3=-1")
d19$laws_toofavorable <- (d19$LEGISLATIONR2rec + d19$LEGISLATIONR3rec + d19$LEGISLATIONR10rec)/3
# second laws favorable scale (wealthy people, large corps) 
d19$LEGISLATIONR8rec <- recode(d19$LEGISLATIONR8, "1=1;2=0;3=-1")
d19$LEGISLATIONR13rec <- recode(d19$LEGISLATIONR13, "1=1;2=0;3=-1")
d19$laws_toofavorable_wealthycorps <- (d19$LEGISLATIONR8rec + d19$LEGISLATIONR13rec)/2
# racial resentment scale
d19$Q16R2_rec <- recode(d19$Q16R2, "1=1;2=1;3=0;4=-1;5=-1")
d19$Q16R3_rec <- recode(d19$Q16R3, "1=1;2=1;3=0;4=-1;5=-1")
d19$Q16R1_rec <- recode(d19$Q16R1, "1=-1;2=-1;3=0;4=1;5=1")
d19$Q16R4_rec <- recode(d19$Q16R4, "1=-1;2=-1;3=0;4=1;5=1")
d19$racial_resent_scale <- (d19$Q16R1_rec + d19$Q16R2_rec + d19$Q16R3_rec + d19$Q16R4_rec)/4


#### saving out recoded dataset for analyses 
write.table(d19, "cleaned_datasets/harris_feb2019.csv", row.names=F, sep=",")